var posTime = "";
var posNumber = "";
var posAmount = "";
var posDetail = "";
var isOperating = false;


//用户管理加载
function load() {
	var message = $('#message').val();
	if (message != "" && message != null) {
		alert(message);
	}
	if($('#showErrorDiv').val()!= true){
		$('.errorData').hide();
	}
	search();
	buttonAuthor();
}

// 查询用户
function search() {
	showLoading();
	var currentPage = $('#gotoPage').val();
	var transactionTime = $('#transactionTimeStr').val();
	var siteKey = $('#siteKey').val();
	var siteName = $('#siteName').val();
	$.ajax( {
		type : "GET",
		contentType : "application/json",
		data : {
			currentPage : currentPage,
			transactionTime : transactionTime,
			siteKey : siteKey,
			siteName : encodeURI(siteName)
		},
		dataType : 'json',
		url : "ajaxPosDetailList",
		async : true,
		success : function(msgs) {
			var date = $('#transactionTimeStr').val().substring(0,10);
			
			$('#leftFY').show();
			$('#rightFY').show();
			$('#myTable tr:not(:first)').empty();
			var posList = msgs.posList;
			$('#curpage').html(msgs.currentPage);
			$('#allpage').html(msgs.maxPage);
			if (msgs.maxPage == 1) {
				$('#leftFY').hide();
				$('#rightFY').hide();
			}
			if (msgs.currentPage == 1) {
				$('#leftFY').hide();
			}
			if (msgs.currentPage == msgs.maxPage) {
				$('#rightFY').hide();
			}
			// 获取语言包
			var language = msgs.language;
			// 获取当前登录用户名
			var recentUserName = msgs.recentUserName;
			
			var firstRow = "<tr><td>"+siteKey+"</td><td>"+siteName+"</td>"
							+ "<td class='posDate' id='posDate' value='"+date+"'>"
							+ date
							+ "</td><td class='posTime' value='addid' id='posTime'>"
							+ "-"
							+ "</td><td class='posNumber' value='addid'>"
							+ "-"
							+ "</td><td class='posAmount' value='addid'>"
							+ "-"
							+ "</td><td class='posDetail' value='addid'>"
							+ "-"
							+ "</td><td><div class='editdiv' value='addid'>"
							+ "<a class='fontOrange author_add' onclick=addPosDataDetail('addid','"
							+ siteKey
							+ "');  href='#'><span>"
							+ "新增"
							+ "</span></div></td></tr>";
			$('#myTable').append(firstRow);
			
			for ( var i = 0; i < posList.length; i++) {
				
				var tempSite = posList[i];
				var tempTime = new Date(tempSite.transactionTime);
				var mouth = tempTime.getMonth()+1;
				if(mouth<10){
					mouth="0"+mouth;
				}
				var day = tempTime.getDate();
				if(day<10){
					day="0"+day;
				}
				var posDate = tempTime.getFullYear()+"-"+mouth+"-"+day;
				var hour = tempTime.getHours();
				if(hour<10){
					hour="0"+hour;
				}
				var min = tempTime.getMinutes();
				if(min<10){
					min="0"+min;
				}
				var sec = tempTime.getSeconds();
				if(sec<10){
					sec="0"+sec;
				}
				var posTime = hour+":"+min+":"+sec;
				var edit = "<a href='#' class='fontOrange author_edit' onclick='editPosDataDetail(&quot;"+tempSite.id
							+"&quot;);"
							+ "'<span>"+"编辑"+"</span> </a>";
				
				var del = "<a href='#' class='fontOrange author_delete' onclick='deletePosDataDetail(&quot;"+tempSite.id
							+"&quot;);"  
							+ "'<span>"+"删除"+"</span> </a>";
				// 根据权限拼接按钮
				var anniu = "";
				anniu=edit+del;
				$('#myTable')
						.append(
								"<tr>"
										+ "<td>"
										+ tempSite.siteKey
										+ "</td><td>"
										+ tempSite.siteName
										+ "</td><td class='posDate' id='posDate' value='"+tempSite.id+"'>"
										+ posDate
										+ "</td><td class='posTime' id='posTime' value='"+tempSite.id+"'>"
										+ posTime
										+ "</td><td class='posNumber' value='"+tempSite.id+"'>"
										+ commafy(tempSite.transactionNumber)
										+ "</td><td class='posAmount' value='"+tempSite.id+"'>"
										+ tempSite.salesAmount
										+ "</td><td class='posDetail' value='"+tempSite.id+"'>"
										+ tempSite.details
										+ "</td><td><div class='editdiv' value='"+tempSite.id+"'>"
										+anniu
										+ "</div></td></tr>");
			}
			hideLoading();
		},
		error : function(mm, a, b, c, d, e) {
			hideLoading();
			myAlert("提示", "服务器好像开小差了，刷新一下试试吧！");
		}
	});

}

function buttonAuthor(){
	var author = $('#author').val();
	if(author != null && ""!=author){
		if(author.indexOf("导入") != -1){
			$('.author_delete').each(function(){
				$(this).show();
			});
			
			$('.author_add').each(function(){
				$(this).show();
			})
			$('.author_view').each(function(){
				$(this).show();
			})
		}
		if(author.indexOf("修改") != -1){
			$('.author_edit').each(function(){
				$(this).show();
			});
			$('.author_add').each(function(){
				$(this).show();
			})
			$('.author_delete').each(function(){
				$(this).show();
			})
		}

	}
}

///分页栏
function gotopage(i) {
	showLoading();
	// 纯数字正则
	var numRule = /(^\d*$)|(^\S+\s+\S+$)/;
	var j = 1;
	var maxPage = parseInt($("#allpage").html());
	// 首页
	if (i == 0) {
		j = 1;
	}
	// 下一页
	else if (i == 1) {
		j = parseInt($("#curpage").html()) + 1;
	}
	// 上一页
	else if (i == 2) {
		j = parseInt($("#curpage").html()) - 1;
	}
	// 跳转
	else if (i == 3) {
		var gotoPage = $("#txtpages").val().trim();
		if (gotoPage == "") {
			j = 1;
		} else if (!gotoPage.match(numRule)) {
			j == 1
		} else {
			j = parseInt(gotoPage);
		}
	}
	// 尾页
	else if (i == -1) {
		j = maxPage;
	}
	if (j < 1) {
		j = 1;
	}
	if (j > maxPage) {
		j = maxPage;
	}
	$("#gotoPage").val(j);
	search();

	
}


function addPosDataDetail(id,siteKey){

	if(!isOperating){
		isOperating = true;
	
	$('.editdiv').each(function(){
		if(id == $(this).attr("value")){
			$(this).children("a").remove();
			var cancelHtml = "<a onclick="+"\""+"cancelAdd('"+id+"');"+"\""+
				"class='fontBlue' href='#'><span>取消</span> </a>"
			$(this).append(cancelHtml);
			var confirmHtml = "<a onclick="+"\""+"confirmAdd(this);"+"\""+
			"class='fontBlue' href='#' ><span>确定</span> </a>"
			$(this).append(confirmHtml);
		}
	});
	
	
	$('.posTime').each(function(){
		if(id == $(this).attr("value")){
			$(this).text("");
			var inputHtml = "<input type='text' onclick='_SetTime(this)' style='width:80px' />"
			$(this).append(inputHtml);
		}
	});
	
	$('.posNumber').each(function(){
		if(id == $(this).attr("value")){
			$(this).text("");
			var inputHtml = "<input type='text' maxlength='8' style='width:50px' maxlength='500' id='txtPosNumber' />"
			$(this).append(inputHtml);
		}
	});
	
	$('.posAmount').each(function(){
		if(id == $(this).attr("value")){
			$(this).text("");
			var inputHtml = "<input type='text' maxlength='12' style='width:50px' id='txtPosAmount' />"
			$(this).append(inputHtml);
		}
	});
	
	$('.posDetail').each(function(){
		if(id == $(this).attr("value")){
			$(this).text("");
			var inputHtml = "<input type='text' style='width:50px' maxlength='500' id='txtPosdetail' />"
			$(this).append(inputHtml);
		}
	});
	}
}


function cancelAdd(id){
	$('.editdiv').each(function(){
		if(id == $(this).attr("value")){
			$(this).children("a").remove();
			var addHtml =	"<a onclick="+"\""+"addPosDataDetail('"+id+"');"+"\""+
				"class='fontOrange author_add' href='#'><span>新增</span> </a>"
			$(this).append(addHtml);
		}
	});
	
	$('.posTime').each(function(){
		if(id == $(this).attr("value")){
			var txt = $(this).children("input").attr("value");
			$(this).children("input").remove();
			$(this).text("-");
		}
	});
	
	$('.posNumber').each(function(){
		if(id == $(this).attr("value")){
			var txt = $(this).children("input").attr("value");
			$(this).children("input").remove();
			$(this).text("-");
		}
	});
	
	$('.posAmount').each(function(){
		if(id == $(this).attr("value")){
			var txt = $(this).children("input").attr("value");
			$(this).children("input").remove();
			$(this).text("-");
		}
	});
	
	$('.posDetail').each(function(){
		if(id == $(this).attr("value")){
			var txt = $(this).children("input").attr("value");
			$(this).children("input").remove();
			$(this).text("-");
		}
	});
	if(isOperating){
		isOperating =false;
	}
}

function confirmAdd(element){
	var number = 0;
	var amount = 0;
	var details = "";
	var flag = true;
	var posTime = "";
	var posDate = $("#posDate").attr("value");
	
	
	var ediv = $(element).parent();
	var tdList = $(ediv).parent().parent().children();
	var siteKey = $(tdList[0]).text().trim();
	
	
	var $posNumber = $(tdList[4]);
	var posTxt = $posNumber.children("input").attr("value");
	posTxt = posTxt == undefined ? posNumber : posTxt;
	if(!isNumber(posTxt)){
		flag = false;
		alert("请输入正确的数据");
		return;
	}
	
	var $posAmount = $(tdList[5]);
	var amoutTxt = $posAmount.children("input").attr("value");
	amoutTxt = amoutTxt == undefined ? posAmount : amoutTxt;
	if(!isDecimal(amoutTxt)){
		flag = false;
		alert("请输入正确的数据");
		return;
	}
	
	
	if(!flag){
		alert("请输入正确的数据");
		return;
	}
	
	var $posDetails = $(tdList[6]);
	details = $posDetails.children("input").attr("value");
	$posDetails.children("input").remove();
	$posDetails.text(details);
	
	var $posTime = $(tdList[3]);
	posTime = $posTime.children("input").attr("value");
	$posTime.children("input").remove();
	$posTime.text(posTime);
	
	number = posTxt;
	$posNumber.children("input").remove();
	$posNumber.text(commafy(posTxt));
	
	amount = amoutTxt;
	$posAmount.children("input").remove();
	$posAmount.text(amoutTxt);
	
	var transcationTime = posDate + " " + posTime;
	
	if(!flag){
		alert("请输入正确的数据");
		return;
	}
	
	
	if(flag){
		$(ediv).children("a").remove();
		var editHtml = 	"<a onclick="+"\""+"editPosData(this);"+"\""+
			"class='fontOrange author_edit' href='#'><span>编辑</span> </a>"
		$(ediv).append(editHtml);
		var deleteHtml = "<a onclick="+"\""+"deletePosData(this);"+"\""+
			"class='fontOrange author_delete' href='#'><span>删除</span> </a>"
		$(ediv).append(deleteHtml);
	}
	
	$.ajax({
		url:"addPosDataDetail",
		type:"post",
		dataType:"json",
		async:false,
        data: { //发送给数据库的数据
			siteKey : siteKey,
		 	transactionNumber : number,
		 	salesAmount : amount,
		 	details : details,
		 	transactionTime : transcationTime
		 },
		success:function(msg){
			 location.reload();
		},
		error: function (XMLHttpRequest, textStatus, thrownError) {
			
			location.reload();
		}
	});
	
	if(isOperating){
		isOperating =false;
	}
}


function editPosDataDetail(id){

	if(!isOperating){
		isOperating = true;
	
	$('.editdiv').each(function(){
		if(id == $(this).attr("value")){
			$(this).children("a").remove();
			var cancelHtml = "<a onclick="+"\""+"cancelEdit('"+id+"');"+"\""+
				"class='fontBlue' href='#'><span>取消</span> </a>"
			$(this).append(cancelHtml);
			var confirmHtml = "<a onclick="+"\""+"confirmEdit(this,'"+id+"');"+"\""+
			"class='fontBlue' href='#' ><span>确定</span> </a>"
			$(this).append(confirmHtml);
		}
	});
	
	$('.posTime').each(function(){
		if(id == $(this).attr("value")){
			var txt = $(this).text().trim();
			posTime = txt;
			if(txt == "-"){
				txt = "";
			}
			$(this).text("");
			var inputHtml = "<input type='text' onclick='_SetTime(this)' style='width:60px'	value='" + txt +"' />";
			$(this).append(inputHtml);
		}
	});
	
	$('.posNumber').each(function(){
		if(id == $(this).attr("value")){
			var txt = commafyback($(this).text().trim());
			posNumber = txt;
			if(txt == "-"){
				txt = "";
			}
			$(this).text("");
			var inputHtml = "<input type='text' style='width:50px' maxlength='8' id='txtPosNumber' value='"+ txt +"' />"
			$(this).append(inputHtml);
		}
	});
	
	$('.posAmount').each(function(){
		if(id == $(this).attr("value")){
			var txt = $(this).text().trim();
			posAmount = txt;
			if(txt == "-"){
				txt = "";
			}
			$(this).text("");
			var inputHtml = "<input type='text' style='width:50px' maxlength='12'  id='txtPosAmount' value='"+ txt +"' />"
			$(this).append(inputHtml);
		}
	});
	
	$('.posDetail').each(function(){
		if(id == $(this).attr("value")){
			var txt = $(this).text().trim();
			posDetail = txt;
			if(txt == "-"){
				txt = "";
			}
			$(this).text("");
			var inputHtml = "<input type='text' style='width:50px' id='txtPosdetail' maxlength='500' value='"+ txt +"' />"
			$(this).append(inputHtml);
		}
	});
	}
}


function cancelEdit(id){
	$('.editdiv').each(function(){
		if(id == $(this).attr("value")){
			$(this).children("a").remove();
			var editHtml =	"<a onclick="+"\""+"editPosDataDetail('"+id+"');"+"\""+
				"class='fontOrange author_edit' href='#'><span>编辑</span> </a>"
			$(this).append(editHtml);
			var deleteHtml =	"<a onclick="+"\""+"deletePosDataDetail('"+id+"');"+"\""+
			"class='fontOrange author_delete' href='#'><span>删除</span> </a>"
		$(this).append(deleteHtml);
		}
	});
	
	$('.posTime').each(function(){
		if(id == $(this).attr("value")){
			var txt = $(this).children("input").attr("value");
			$(this).children("input").hide();
			$(this).text(posTime);
		}
	});
	
	$('.posNumber').each(function(){
		if(id == $(this).attr("value")){
			var txt = $(this).children("input").attr("value");
			$(this).children("input").remove();
			$(this).text(commafy(posNumber));
		}
	});
	
	$('.posAmount').each(function(){
		if(id == $(this).attr("value")){
			var txt = $(this).children("input").attr("value");
			$(this).children("input").remove();
			$(this).text(posAmount);
		}
	});
	
	$('.posDetail').each(function(){
		if(id == $(this).attr("value")){
			var txt = $(this).children("input").attr("value");
			$(this).children("input").remove();
			$(this).text(posDetail);
		}
	});
	
	if(isOperating){
		isOperating = false;
	}
	
}

function confirmEdit(element,id){
	var number = 0;
	var amount = 0;
	var details = "";
	var flag = true;
	var posTime = "";
	var posDate = $("#posDate").attr("value");
	
	
	var ediv = $(element).parent();
	var tdList = $(ediv).parent().parent().children();
	var siteKey = $(tdList[0]).text().trim();
	
	
	var $posNumber = $(tdList[4]);
	var posTxt = $posNumber.children("input").attr("value");
	posTxt = posTxt == undefined ? posNumber : posTxt;
	if(!isNumber(posTxt)){
		flag = false;
		alert("请输入正确的数据");
		return;
	}
	
	var $posAmount = $(tdList[5]);
	var amoutTxt = $posAmount.children("input").attr("value");
	amoutTxt = amoutTxt == undefined ? posAmount : amoutTxt;
	if(!isDecimal(amoutTxt)){
		flag = false;
		alert("请输入正确的数据");
		return;
	}
	
	
	if(!flag){
		alert("请输入正确的数据");
		return;
	}
	
	var $posDetails = $(tdList[6]);
	details = $posDetails.children("input").attr("value");
	$posDetails.children("input").remove();
	$posDetails.text(details);
	
	var $posTime = $(tdList[3]);
	posTime = $posTime.children("input").attr("value");
	$posTime.children("input").remove();
	$posTime.text(posTime);
	
	number = posTxt;
	$posNumber.children("input").remove();
	$posNumber.text(commafy(posTxt));
	
	amount = amoutTxt;
	$posAmount.children("input").remove();
	$posAmount.text(amoutTxt);
	
	var transcationTime = posDate + " " + posTime;
	
	if(!flag){
		alert("请输入正确的数据");
		return;
	}
	
	
	if(flag){
		$(ediv).children("a").remove();
		var editHtml = 	"<a onclick="+"\""+"editPosData(this);"+"\""+
			"class='fontOrange author_edit' href='#'><span>编辑</span> </a>"
		$(ediv).append(editHtml);
		var deleteHtml = "<a onclick="+"\""+"deletePosData(this);"+"\""+
			"class='fontOrange author_delete' href='#'><span>删除</span> </a>"
		$(ediv).append(deleteHtml);
	}
		
		var transactionTime = posDate + " " + posTime;
		
		$.ajax({
			url:"editPosDataDetail",
			type:"post",
			dataType:"json",
			async:true,
	        data: { //发送给数据库的数据
				id : id,
			 	transactionNumber : number,
			 	salesAmount : amount,
			 	details : details,
			 	transactionTime : transactionTime
			 },
			success:function(msg){
				alert("编辑POS数据成功");
			},
			error: function (XMLHttpRequest, textStatus, thrownError) {
				alert("编辑POS数据失败");
			}
		});
	

	
	if(isOperating){
		isOperating = false;
	}
}


function deletePosDataDetail(id){
	if(confirm("您确定要执行该操作吗？")){
		var posDate = $("#posDate").attr("value");
		
		$('.posNumber').each(function(){
			if(id == $(this).attr("value")){
				$(this).parent().remove();
			}
		});
		
		$.ajax({
			url:"deletePosDataDetail",
			type:"post",
			dataType:"json",
			async:false,
	        data: { //发送给数据库的数据
				id : id,
			 	posDate : posDate
			 },
			success:function(msg){
			},
			error: function (XMLHttpRequest, textStatus, thrownError) {
			}
		
		});
	}
}
function back(){
	window.location.href = "posManager";
}

//数字的千分位表示
function commafy(num) 
{ 
	var result = '', counter = 0;
	num = (num || 0).toString();
	for (var i = num.length - 1; i >= 0; i--) {
	counter++;
	result = num.charAt(i) + result;
	if (!(counter % 3) && i != 0) { result = ',' + result; }
	}
	return result; 
} 

// 去除千分位
function commafyback(num) 
{ 
	var x = num.split(','); 
	return parseFloat(x.join("")); 
}
